package klimuts.periodicals.admin.actions;

import klimuts.periodicals.catalog.Periodical;
import klimuts.periodicals.data.DataSource;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Action add new periodical to data base
 * @author Siarhei Klimuts
 */
public class AddPeriodical extends ActionSupport {
	private String name;
	private String description;
	private float price;
	private byte type;
	
	public String execute() throws Exception {
		if (DataSource.addPeriodical(new Periodical(name, description, type, price)))
			return "success";
		else {
			addActionError(getText("admin.periodicals.error.add"));
			return "input";
		}
	}
	
	public void validate() {
		if (isOutOfRange(name, 3, 20))
			addFieldError("name", getText("periodical.name.incorrect"));
		if (isOutOfRange(description, 10, 100))
			addFieldError("description", getText("periodical.description.incorrect"));
	}
	
	private boolean isOutOfRange(String str, int l1, int l2){
		return ((str.length()<l1)||(str.length()>l2));
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public float getPrice() {
		return price;
	}
	
	public void setPrice(float price) {
		this.price = price;
	}
	
	public byte getType() {
		return type;
	}
	
	public void setType(byte type) {
		this.type = type;
	}

}
